package com.wanlian.socketserver.Device.worker;

import com.wanlian.common.KeyPara;
import com.wanlian.json.JsonStringUtil;
import com.wanlian.util.HttpUtils;

import java.util.concurrent.BlockingQueue;

public class PostWorker implements Runnable {
    private BlockingQueue<KeyPara> queue;

    public PostWorker(BlockingQueue<KeyPara> queue) {
        this.queue = queue;
    }

    @Override
    public void run() {
        System.out.println("消费线程启动...");
        try {
            while (true) {
                try {
                    for (int i = 0; i < 100; i++) {
                        KeyPara data = queue.poll();
                        if (data != null) {
                           // System.out.println("消费者,拿到队列中的数据data:" + JsonStringUtil.writeValueAsString(data));
                            HttpUtils.sendPost(data.getKey(), data.getValue());
                        }
                    }
                    Thread.sleep(100);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            System.out.println("消费者退出线程...");
        }

    }

}